{ TIfxDiagnostics }

  //!!!TIfxDiagnosticsItemType = (ditSuccess, ditWarning, ditError);

  TIfxDiagnosticsItem = class
  private
    FReturnedSqlState: string;
    FClassOrigin: string;
    FSubClassOrigin: string;
    FMessageText: string;
    FServerName: string;
    FConnectionName: string;
    FInformixSqlCode: int4;
  public
    function IsSuccess: Boolean;
    function IsWarning: Boolean;
    function IsError: Boolean;
    property ReturnedSqlState: string read FReturnedSqlState;
    property ClassOrigin: string read FClassOrigin;
    property SubClassOrigin: string read FSubClassOrigin;
    property MessageText: string read FMessageText;
    property ServerName: string read FServerName;
    property ConnectionName: string read FConnectionName;
    property InformixSqlCode: int4 read FInformixSqlCode;
  end;

  TIfxDiagnostics = class
  private
    FMore: Boolean;
    FRowCount: mint;
    FItems: TList;
    function GetNumber: mint;
    function GetItem(Index: mint): TIfxDiagnosticsItem;
  public
    constructor Create;
    destructor Destroy; override;
    property Number: mint read GetNumber;
    property More: Boolean read FMore;
    property RowCount: mint read FRowCount;
    property Items[Index: mint]: TIfxDiagnosticsItem read GetItem; default;
  end;


function TIfxDiagnosticsItem.IsSuccess: Boolean;
begin
  Result := (FReturnedSqlState[1] = '0') and (FReturnedSqlState[2] in ['0', '2']);
end;

function TIfxDiagnosticsItem.IsWarning: Boolean;
begin
  Result := (FReturnedSqlState[1] = '0') and (FReturnedSqlState[2] = '1');
end;

function TIfxDiagnosticsItem.IsError: Boolean;
begin
  Result := not (IsSuccess or IsWarning);
end;

constructor TIfxDiagnostics.Create;
var
  HostVars: array [0..8] of TIfx_HostVar;

  Number: Integer;
  More: array [1..2] of Char;

  I: Integer;
  
  ReturnedSqlState: array [1..6] of char;
  ClassOrigin: array [1..255] of char;
  SubClassOrigin: array [1..255] of char;
  MessageText: array [1..8191] of char;
  MessageLength: mint;
  ServerName: array [1..255] of char;
  ConnectionName: array [1..255] of char;
  InformixSqlCode: int4;

  Item: TIfxDiagnosticsItem;

  ApiResult: mint;

  procedure InitHostVar(var HostVar: TIfx_HostVar; hostaddr: PChar;
    fieldtype: int2; hosttype: int2; hostlen: int4);
  begin
    HostVar.hostaddr := hostaddr;
    HostVar.fieldtype := fieldtype;
    HostVar.hosttype := hosttype;
    HostVar.hostlen := hostlen;
  end;

begin
  inherited;

  FItems := TList.Create;

  Fillchar(HostVars, SizeOf(HostVars), 0);

  InitHostVar(HostVars[0], @Number, DIAGS_NUMBER_FIELD, CINTTYPE, SizeOf(Number));
  InitHostVar(HostVars[1], @More, DIAGS_MORE_FIELD, CCHARTYPE, SizeOf(More));
  InitHostVar(HostVars[2], @FRowCount, DIAGS_ROW_COUNT_FIELD, CINTTYPE, SizeOf(FRowCount));

  ApiResult := sqli_diag_get(ESQLINTVERSION, @HostVars, -1);
  if ApiResult = -1 then Exit;
  FMore := More = 'Y';

  InitHostVar(HostVars[0], @ReturnedSqlState, DIAGS_RETURNED_SQLSTATE_FIELD, CCHARTYPE, SizeOf(ReturnedSqlState));
  InitHostVar(HostVars[1], @ClassOrigin, DIAGS_CLASS_ORIGIN_FIELD, CCHARTYPE, SizeOf(ClassOrigin));
  InitHostVar(HostVars[2], @SubClassOrigin, DIAGS_SUBCLASS_ORIGIN_FIELD, CCHARTYPE, SizeOf(SubClassOrigin));
  InitHostVar(HostVars[3], @MessageText, DIAGS_MESSAGE_TEXT_FIELD, CCHARTYPE, SizeOf(MessageText));
  InitHostVar(HostVars[4], @MessageLength, DIAGS_MESSAGE_LENGTH_FIELD, CINTTYPE, SizeOf(MessageLength));
  InitHostVar(HostVars[5], @ServerName, DIAGS_SERVER_NAME_FIELD, CCHARTYPE, SizeOf(ServerName));
  InitHostVar(HostVars[6], @ConnectionName, DIAGS_CONNECTION_NAME_FIELD, CCHARTYPE, SizeOf(ConnectionName));
  InitHostVar(HostVars[7], @InformixSqlCode, DIAGS_INFORMIX_SQLCODE_FIELD, CINTTYPE, SizeOf(InformixSqlCode));

  for I := 1 to Number do
  begin
    ApiResult := sqli_diag_get(ESQLINTVERSION, @HostVars, I);
    Item := TIfxDiagnosticsItem.Create;
    Item.FReturnedSqlState := TrimRight(ReturnedSqlState);
    Item.FClassOrigin := TrimRight(ClassOrigin);
    Item.FSubClassOrigin := TrimRight(SubClassOrigin);
    Item.FMessageText := MessageText;
    SetLength(Item.FMessageText, MessageLength);
    Item.FServerName := TrimRight(ServerName);
    Item.FConnectionName := TrimRight(ConnectionName);
    Item.FInformixSqlCode := InformixSqlCode;
    FItems.Add(Item);
  end;
end;

destructor TIfxDiagnostics.Destroy;
var
  I: Integer;
begin
  if FItems <> nil then
  begin
    for I := 0 to FItems.Count - 1 do
      TIfxDiagnosticsItem(FItems[I]).Free;
    FItems.Free;
  end;
  inherited;
end;

function TIfxDiagnostics.GetNumber: mint;
begin
  Result := FItems.Count;
end;

function TIfxDiagnostics.GetItem(Index: mint): TIfxDiagnosticsItem;
begin
  Result := FItems[Index];
end;
